package com.getpebble.android.framework.protocol.outbound;

import com.getpebble.android.framework.appmessage.AppMessage;
import com.getpebble.android.framework.appmessage.AppMessage.AppMessageCommand;
import com.getpebble.android.framework.appmessage.BinaryPebbleDictionary;
import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.framework.util.ByteUtils;

public class PblOutboundAppMessage extends PblOutboundMessage {
    private AppMessage mAppMessage;

    public PblOutboundAppMessage(AppMessage appMessage) {
        this(EndpointId.APP_MESSAGE, appMessage);
    }

    protected PblOutboundAppMessage(EndpointId endpointId, AppMessage appMessage) {
        super(endpointId);
        this.mAppMessage = appMessage;
    }

    public AppMessage getAppMessage() {
        return this.mAppMessage;
    }

    public synchronized byte[] getBytes() {
        if (!areBytesCached()) {
            addBytes(Byte.valueOf(this.mAppMessage.getCommand().getCode()));
            addBytes(Byte.valueOf(this.mAppMessage.getTransactionId()));
            if (!(this.mAppMessage.getCommand() == AppMessageCommand.ACK || this.mAppMessage.getCommand() == AppMessageCommand.NACK)) {
                addBytes(ByteUtils.uuid2bytes(this.mAppMessage.getUuid()));
                addBytes(new BinaryPebbleDictionary(this.mAppMessage.getPebbleDictionary()).toByteBuffer().array());
            }
        }
        return super.getBytes();
    }
}
